
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <util/delay.h>
#include <string.h>

#include "LedControl.h"
#include "adc.h"
#include "spi.h"
#include "rf12.h"
#include "pwm.h"
//#include "command_procesor.h"
#include "tickcount.h"


unsigned char *packet;
unsigned char packet_len;
volatile int pc=0;
int timeout;
uint16_t tc_last_packet;
unsigned char green_led_state=0,red_led_state=0;


int main(void)
{
	PWM_Init();
	prepare_adc_measure();
	TickcountPrepare();
	LedInit();	
	Spi_Init();
	RF12_Init();



	sei(); /* enable interrupt */


	timeout=100;

	tc_last_packet=GetTickCount();


	RFM12B_SetReceiveMode();

	while(1) 
	{

		if(RFM12B_ReadPacket(&packet,&packet_len)==2)
		{
			GreenLED(1);
			timeout=100;
			tc_last_packet=GetTickCount();
			ProcessCommand(packet,packet_len);
			RedLED(0);
			pc++;
		}
		
		if(timeout==0 ) GreenLED(0); else timeout--;

		if(GetTickSince(tc_last_packet)>500)
		{
			tc_last_packet=GetTickCount()+1000;
			EmergencyAbort();
			RedLED(1);
		} 

	}




return 0;

}



